<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>proto_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_proto2_proto_test.js.html">proto_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.proto2.messageTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.proto2.messageTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.proto2.FieldDescriptor&#39;);
<a name="line19"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line20"></a>goog.require(&#39;proto2.TestAllTypes&#39;);
<a name="line21"></a>goog.require(&#39;someprotopackage.TestPackageTypes&#39;);
<a name="line22"></a>
<a name="line23"></a>function testPackage() {
<a name="line24"></a>  var message = new someprotopackage.TestPackageTypes();
<a name="line25"></a>  message.setOptionalInt32(45);
<a name="line26"></a>  message.setOtherAll(new proto2.TestAllTypes());
<a name="line27"></a>}
<a name="line28"></a>
<a name="line29"></a>function testFields() {
<a name="line30"></a>  var message = new proto2.TestAllTypes();
<a name="line31"></a>
<a name="line32"></a>  // Ensure that the fields are not set.
<a name="line33"></a>  assertFalse(message.hasOptionalInt32());
<a name="line34"></a>  assertFalse(message.hasOptionalInt64());
<a name="line35"></a>  assertFalse(message.hasOptionalUint32());
<a name="line36"></a>  assertFalse(message.hasOptionalUint64());
<a name="line37"></a>  assertFalse(message.hasOptionalSint32());
<a name="line38"></a>  assertFalse(message.hasOptionalSint64());
<a name="line39"></a>  assertFalse(message.hasOptionalFixed32());
<a name="line40"></a>  assertFalse(message.hasOptionalFixed64());
<a name="line41"></a>  assertFalse(message.hasOptionalSfixed32());
<a name="line42"></a>  assertFalse(message.hasOptionalSfixed64());
<a name="line43"></a>  assertFalse(message.hasOptionalFloat());
<a name="line44"></a>  assertFalse(message.hasOptionalDouble());
<a name="line45"></a>  assertFalse(message.hasOptionalBool());
<a name="line46"></a>  assertFalse(message.hasOptionalString());
<a name="line47"></a>  assertFalse(message.hasOptionalBytes());
<a name="line48"></a>  assertFalse(message.hasOptionalgroup());
<a name="line49"></a>  assertFalse(message.hasOptionalNestedMessage());
<a name="line50"></a>  assertFalse(message.hasOptionalNestedEnum());
<a name="line51"></a>
<a name="line52"></a>  // Check non-set values.
<a name="line53"></a>  assertNull(message.getOptionalInt32());
<a name="line54"></a>  assertNull(message.getOptionalInt64());
<a name="line55"></a>  assertNull(message.getOptionalFloat());
<a name="line56"></a>  assertNull(message.getOptionalString());
<a name="line57"></a>  assertNull(message.getOptionalBytes());
<a name="line58"></a>  assertNull(message.getOptionalNestedMessage());
<a name="line59"></a>  assertNull(message.getOptionalNestedEnum());
<a name="line60"></a>
<a name="line61"></a>  // Check default values.
<a name="line62"></a>  assertEquals(0, message.getOptionalInt32OrDefault());
<a name="line63"></a>  assertEquals(&#39;1&#39;, message.getOptionalInt64OrDefault());
<a name="line64"></a>  assertEquals(1.5, message.getOptionalFloatOrDefault());
<a name="line65"></a>  assertEquals(&#39;&#39;, message.getOptionalStringOrDefault());
<a name="line66"></a>  assertEquals(&#39;moo&#39;, message.getOptionalBytesOrDefault());
<a name="line67"></a>
<a name="line68"></a>  // Set the fields.
<a name="line69"></a>  message.setOptionalInt32(101);
<a name="line70"></a>  message.setOptionalInt64(&#39;102&#39;);
<a name="line71"></a>  message.setOptionalUint32(103);
<a name="line72"></a>  message.setOptionalUint64(&#39;104&#39;);
<a name="line73"></a>  message.setOptionalSint32(105);
<a name="line74"></a>  message.setOptionalSint64(&#39;106&#39;);
<a name="line75"></a>  message.setOptionalFixed32(107);
<a name="line76"></a>  message.setOptionalFixed64(&#39;108&#39;);
<a name="line77"></a>  message.setOptionalSfixed32(109);
<a name="line78"></a>  message.setOptionalSfixed64(&#39;110&#39;);
<a name="line79"></a>  message.setOptionalFloat(111.5);
<a name="line80"></a>  message.setOptionalDouble(112.5);
<a name="line81"></a>  message.setOptionalBool(true);
<a name="line82"></a>  message.setOptionalString(&#39;test&#39;);
<a name="line83"></a>  message.setOptionalBytes(&#39;abcd&#39;);
<a name="line84"></a>
<a name="line85"></a>  var group = new proto2.TestAllTypes.OptionalGroup();
<a name="line86"></a>  group.setA(111);
<a name="line87"></a>
<a name="line88"></a>  message.setOptionalgroup(group);
<a name="line89"></a>
<a name="line90"></a>  var nestedMessage = new proto2.TestAllTypes.NestedMessage();
<a name="line91"></a>  nestedMessage.setB(112);
<a name="line92"></a>
<a name="line93"></a>  message.setOptionalNestedMessage(nestedMessage);
<a name="line94"></a>
<a name="line95"></a>  message.setOptionalNestedEnum(proto2.TestAllTypes.NestedEnum.FOO);
<a name="line96"></a>
<a name="line97"></a>  // Ensure that the fields are set.
<a name="line98"></a>  assertTrue(message.hasOptionalInt32());
<a name="line99"></a>  assertTrue(message.hasOptionalInt64());
<a name="line100"></a>  assertTrue(message.hasOptionalUint32());
<a name="line101"></a>  assertTrue(message.hasOptionalUint64());
<a name="line102"></a>  assertTrue(message.hasOptionalSint32());
<a name="line103"></a>  assertTrue(message.hasOptionalSint64());
<a name="line104"></a>  assertTrue(message.hasOptionalFixed32());
<a name="line105"></a>  assertTrue(message.hasOptionalFixed64());
<a name="line106"></a>  assertTrue(message.hasOptionalSfixed32());
<a name="line107"></a>  assertTrue(message.hasOptionalSfixed64());
<a name="line108"></a>  assertTrue(message.hasOptionalFloat());
<a name="line109"></a>  assertTrue(message.hasOptionalDouble());
<a name="line110"></a>  assertTrue(message.hasOptionalBool());
<a name="line111"></a>  assertTrue(message.hasOptionalString());
<a name="line112"></a>  assertTrue(message.hasOptionalBytes());
<a name="line113"></a>  assertTrue(message.hasOptionalgroup());
<a name="line114"></a>  assertTrue(message.hasOptionalNestedMessage());
<a name="line115"></a>  assertTrue(message.hasOptionalNestedEnum());
<a name="line116"></a>
<a name="line117"></a>  // Ensure that there is a count of 1 for each of the fields.
<a name="line118"></a>  assertEquals(1, message.optionalInt32Count());
<a name="line119"></a>  assertEquals(1, message.optionalInt64Count());
<a name="line120"></a>  assertEquals(1, message.optionalUint32Count());
<a name="line121"></a>  assertEquals(1, message.optionalUint64Count());
<a name="line122"></a>  assertEquals(1, message.optionalSint32Count());
<a name="line123"></a>  assertEquals(1, message.optionalSint64Count());
<a name="line124"></a>  assertEquals(1, message.optionalFixed32Count());
<a name="line125"></a>  assertEquals(1, message.optionalFixed64Count());
<a name="line126"></a>  assertEquals(1, message.optionalSfixed32Count());
<a name="line127"></a>  assertEquals(1, message.optionalSfixed64Count());
<a name="line128"></a>  assertEquals(1, message.optionalFloatCount());
<a name="line129"></a>  assertEquals(1, message.optionalDoubleCount());
<a name="line130"></a>  assertEquals(1, message.optionalBoolCount());
<a name="line131"></a>  assertEquals(1, message.optionalStringCount());
<a name="line132"></a>  assertEquals(1, message.optionalBytesCount());
<a name="line133"></a>  assertEquals(1, message.optionalgroupCount());
<a name="line134"></a>  assertEquals(1, message.optionalNestedMessageCount());
<a name="line135"></a>  assertEquals(1, message.optionalNestedEnumCount());
<a name="line136"></a>
<a name="line137"></a>  // Ensure that the fields have the values expected.
<a name="line138"></a>  assertEquals(101, message.getOptionalInt32());
<a name="line139"></a>  assertEquals(&#39;102&#39;, message.getOptionalInt64());
<a name="line140"></a>  assertEquals(103, message.getOptionalUint32());
<a name="line141"></a>  assertEquals(&#39;104&#39;, message.getOptionalUint64());
<a name="line142"></a>  assertEquals(105, message.getOptionalSint32());
<a name="line143"></a>  assertEquals(&#39;106&#39;, message.getOptionalSint64());
<a name="line144"></a>  assertEquals(107, message.getOptionalFixed32());
<a name="line145"></a>  assertEquals(&#39;108&#39;, message.getOptionalFixed64());
<a name="line146"></a>  assertEquals(109, message.getOptionalSfixed32());
<a name="line147"></a>  assertEquals(&#39;110&#39;, message.getOptionalSfixed64());
<a name="line148"></a>  assertEquals(111.5, message.getOptionalFloat());
<a name="line149"></a>  assertEquals(112.5, message.getOptionalDouble());
<a name="line150"></a>  assertEquals(true, message.getOptionalBool());
<a name="line151"></a>  assertEquals(&#39;test&#39;, message.getOptionalString());
<a name="line152"></a>  assertEquals(&#39;abcd&#39;, message.getOptionalBytes());
<a name="line153"></a>  assertEquals(group, message.getOptionalgroup());
<a name="line154"></a>  assertEquals(nestedMessage, message.getOptionalNestedMessage());
<a name="line155"></a>  assertEquals(proto2.TestAllTypes.NestedEnum.FOO,
<a name="line156"></a>               message.getOptionalNestedEnum());
<a name="line157"></a>}
<a name="line158"></a>
<a name="line159"></a>function testRepeated() {
<a name="line160"></a>  var message = new proto2.TestAllTypes();
<a name="line161"></a>
<a name="line162"></a>  // Ensure that the fields are not set.
<a name="line163"></a>  assertFalse(message.hasRepeatedInt32());
<a name="line164"></a>  assertFalse(message.hasRepeatedInt64());
<a name="line165"></a>  assertFalse(message.hasRepeatedUint32());
<a name="line166"></a>  assertFalse(message.hasRepeatedUint64());
<a name="line167"></a>  assertFalse(message.hasRepeatedSint32());
<a name="line168"></a>  assertFalse(message.hasRepeatedSint64());
<a name="line169"></a>  assertFalse(message.hasRepeatedFixed32());
<a name="line170"></a>  assertFalse(message.hasRepeatedFixed64());
<a name="line171"></a>  assertFalse(message.hasRepeatedSfixed32());
<a name="line172"></a>  assertFalse(message.hasRepeatedSfixed64());
<a name="line173"></a>  assertFalse(message.hasRepeatedFloat());
<a name="line174"></a>  assertFalse(message.hasRepeatedDouble());
<a name="line175"></a>  assertFalse(message.hasRepeatedBool());
<a name="line176"></a>  assertFalse(message.hasRepeatedString());
<a name="line177"></a>  assertFalse(message.hasRepeatedBytes());
<a name="line178"></a>  assertFalse(message.hasRepeatedgroup());
<a name="line179"></a>  assertFalse(message.hasRepeatedNestedMessage());
<a name="line180"></a>  assertFalse(message.hasRepeatedNestedEnum());
<a name="line181"></a>
<a name="line182"></a>  // Expect the arrays to be empty.
<a name="line183"></a>  assertEquals(0, message.repeatedInt32Array().length);
<a name="line184"></a>  assertEquals(0, message.repeatedInt64Array().length);
<a name="line185"></a>  assertEquals(0, message.repeatedUint32Array().length);
<a name="line186"></a>  assertEquals(0, message.repeatedUint64Array().length);
<a name="line187"></a>  assertEquals(0, message.repeatedSint32Array().length);
<a name="line188"></a>  assertEquals(0, message.repeatedSint64Array().length);
<a name="line189"></a>  assertEquals(0, message.repeatedFixed32Array().length);
<a name="line190"></a>  assertEquals(0, message.repeatedFixed64Array().length);
<a name="line191"></a>  assertEquals(0, message.repeatedSfixed32Array().length);
<a name="line192"></a>  assertEquals(0, message.repeatedSfixed64Array().length);
<a name="line193"></a>  assertEquals(0, message.repeatedFloatArray().length);
<a name="line194"></a>  assertEquals(0, message.repeatedDoubleArray().length);
<a name="line195"></a>  assertEquals(0, message.repeatedBoolArray().length);
<a name="line196"></a>  assertEquals(0, message.repeatedStringArray().length);
<a name="line197"></a>  assertEquals(0, message.repeatedBytesArray().length);
<a name="line198"></a>  assertEquals(0, message.repeatedgroupArray().length);
<a name="line199"></a>  assertEquals(0, message.repeatedNestedMessageArray().length);
<a name="line200"></a>  assertEquals(0, message.repeatedNestedEnumArray().length);
<a name="line201"></a>
<a name="line202"></a>  // Set the fields.
<a name="line203"></a>  message.addRepeatedInt32(101);
<a name="line204"></a>  message.addRepeatedInt64(&#39;102&#39;);
<a name="line205"></a>  message.addRepeatedUint32(103);
<a name="line206"></a>  message.addRepeatedUint64(&#39;104&#39;);
<a name="line207"></a>  message.addRepeatedSint32(105);
<a name="line208"></a>  message.addRepeatedSint64(&#39;106&#39;);
<a name="line209"></a>  message.addRepeatedFixed32(107);
<a name="line210"></a>  message.addRepeatedFixed64(&#39;108&#39;);
<a name="line211"></a>  message.addRepeatedSfixed32(109);
<a name="line212"></a>  message.addRepeatedSfixed64(&#39;110&#39;);
<a name="line213"></a>  message.addRepeatedFloat(111.5);
<a name="line214"></a>  message.addRepeatedDouble(112.5);
<a name="line215"></a>  message.addRepeatedBool(true);
<a name="line216"></a>  message.addRepeatedString(&#39;test&#39;);
<a name="line217"></a>  message.addRepeatedBytes(&#39;abcd&#39;);
<a name="line218"></a>
<a name="line219"></a>  message.addRepeatedInt32(201);
<a name="line220"></a>  message.addRepeatedInt64(&#39;202&#39;);
<a name="line221"></a>  message.addRepeatedUint32(203);
<a name="line222"></a>  message.addRepeatedUint64(&#39;204&#39;);
<a name="line223"></a>  message.addRepeatedSint32(205);
<a name="line224"></a>  message.addRepeatedSint64(&#39;206&#39;);
<a name="line225"></a>  message.addRepeatedFixed32(207);
<a name="line226"></a>  message.addRepeatedFixed64(&#39;208&#39;);
<a name="line227"></a>  message.addRepeatedSfixed32(209);
<a name="line228"></a>  message.addRepeatedSfixed64(&#39;210&#39;);
<a name="line229"></a>  message.addRepeatedFloat(211.5);
<a name="line230"></a>  message.addRepeatedDouble(212.5);
<a name="line231"></a>  message.addRepeatedBool(true);
<a name="line232"></a>  message.addRepeatedString(&#39;test#2&#39;);
<a name="line233"></a>  message.addRepeatedBytes(&#39;efgh&#39;);
<a name="line234"></a>
<a name="line235"></a>
<a name="line236"></a>  var group1 = new proto2.TestAllTypes.RepeatedGroup();
<a name="line237"></a>  group1.addA(111);
<a name="line238"></a>
<a name="line239"></a>  message.addRepeatedgroup(group1);
<a name="line240"></a>
<a name="line241"></a>  var group2 = new proto2.TestAllTypes.RepeatedGroup();
<a name="line242"></a>  group2.addA(211);
<a name="line243"></a>
<a name="line244"></a>  message.addRepeatedgroup(group2);
<a name="line245"></a>
<a name="line246"></a>  var nestedMessage1 = new proto2.TestAllTypes.NestedMessage();
<a name="line247"></a>  nestedMessage1.setB(112);
<a name="line248"></a>  message.addRepeatedNestedMessage(nestedMessage1);
<a name="line249"></a>
<a name="line250"></a>  var nestedMessage2 = new proto2.TestAllTypes.NestedMessage();
<a name="line251"></a>  nestedMessage2.setB(212);
<a name="line252"></a>  message.addRepeatedNestedMessage(nestedMessage2);
<a name="line253"></a>
<a name="line254"></a>  message.addRepeatedNestedEnum(proto2.TestAllTypes.NestedEnum.FOO);
<a name="line255"></a>  message.addRepeatedNestedEnum(proto2.TestAllTypes.NestedEnum.BAR);
<a name="line256"></a>
<a name="line257"></a>  // Ensure that the fields are set.
<a name="line258"></a>  assertTrue(message.hasRepeatedInt32());
<a name="line259"></a>  assertTrue(message.hasRepeatedInt64());
<a name="line260"></a>  assertTrue(message.hasRepeatedUint32());
<a name="line261"></a>  assertTrue(message.hasRepeatedUint64());
<a name="line262"></a>  assertTrue(message.hasRepeatedSint32());
<a name="line263"></a>  assertTrue(message.hasRepeatedSint64());
<a name="line264"></a>  assertTrue(message.hasRepeatedFixed32());
<a name="line265"></a>  assertTrue(message.hasRepeatedFixed64());
<a name="line266"></a>  assertTrue(message.hasRepeatedSfixed32());
<a name="line267"></a>  assertTrue(message.hasRepeatedSfixed64());
<a name="line268"></a>  assertTrue(message.hasRepeatedFloat());
<a name="line269"></a>  assertTrue(message.hasRepeatedDouble());
<a name="line270"></a>  assertTrue(message.hasRepeatedBool());
<a name="line271"></a>  assertTrue(message.hasRepeatedString());
<a name="line272"></a>  assertTrue(message.hasRepeatedBytes());
<a name="line273"></a>  assertTrue(message.hasRepeatedgroup());
<a name="line274"></a>  assertTrue(message.hasRepeatedNestedMessage());
<a name="line275"></a>  assertTrue(message.hasRepeatedNestedEnum());
<a name="line276"></a>
<a name="line277"></a>  // Ensure that there is a count of 2 for each of the fields.
<a name="line278"></a>  assertEquals(2, message.repeatedInt32Count());
<a name="line279"></a>  assertEquals(2, message.repeatedInt64Count());
<a name="line280"></a>  assertEquals(2, message.repeatedUint32Count());
<a name="line281"></a>  assertEquals(2, message.repeatedUint64Count());
<a name="line282"></a>  assertEquals(2, message.repeatedSint32Count());
<a name="line283"></a>  assertEquals(2, message.repeatedSint64Count());
<a name="line284"></a>  assertEquals(2, message.repeatedFixed32Count());
<a name="line285"></a>  assertEquals(2, message.repeatedFixed64Count());
<a name="line286"></a>  assertEquals(2, message.repeatedSfixed32Count());
<a name="line287"></a>  assertEquals(2, message.repeatedSfixed64Count());
<a name="line288"></a>  assertEquals(2, message.repeatedFloatCount());
<a name="line289"></a>  assertEquals(2, message.repeatedDoubleCount());
<a name="line290"></a>  assertEquals(2, message.repeatedBoolCount());
<a name="line291"></a>  assertEquals(2, message.repeatedStringCount());
<a name="line292"></a>  assertEquals(2, message.repeatedBytesCount());
<a name="line293"></a>  assertEquals(2, message.repeatedgroupCount());
<a name="line294"></a>  assertEquals(2, message.repeatedNestedMessageCount());
<a name="line295"></a>  assertEquals(2, message.repeatedNestedEnumCount());
<a name="line296"></a>
<a name="line297"></a>  // Ensure that the fields have the values expected.
<a name="line298"></a>  assertEquals(101, message.getRepeatedInt32(0));
<a name="line299"></a>  assertEquals(&#39;102&#39;, message.getRepeatedInt64(0));
<a name="line300"></a>  assertEquals(103, message.getRepeatedUint32(0));
<a name="line301"></a>  assertEquals(&#39;104&#39;, message.getRepeatedUint64(0));
<a name="line302"></a>  assertEquals(105, message.getRepeatedSint32(0));
<a name="line303"></a>  assertEquals(&#39;106&#39;, message.getRepeatedSint64(0));
<a name="line304"></a>  assertEquals(107, message.getRepeatedFixed32(0));
<a name="line305"></a>  assertEquals(&#39;108&#39;, message.getRepeatedFixed64(0));
<a name="line306"></a>  assertEquals(109, message.getRepeatedSfixed32(0));
<a name="line307"></a>  assertEquals(&#39;110&#39;, message.getRepeatedSfixed64(0));
<a name="line308"></a>  assertEquals(111.5, message.getRepeatedFloat(0));
<a name="line309"></a>  assertEquals(112.5, message.getRepeatedDouble(0));
<a name="line310"></a>  assertEquals(true, message.getRepeatedBool(0));
<a name="line311"></a>  assertEquals(&#39;test&#39;, message.getRepeatedString(0));
<a name="line312"></a>  assertEquals(&#39;abcd&#39;, message.getRepeatedBytes(0));
<a name="line313"></a>  assertEquals(group1, message.getRepeatedgroup(0));
<a name="line314"></a>  assertEquals(nestedMessage1, message.getRepeatedNestedMessage(0));
<a name="line315"></a>  assertEquals(proto2.TestAllTypes.NestedEnum.FOO,
<a name="line316"></a>               message.getRepeatedNestedEnum(0));
<a name="line317"></a>
<a name="line318"></a>  assertEquals(201, message.getRepeatedInt32(1));
<a name="line319"></a>  assertEquals(&#39;202&#39;, message.getRepeatedInt64(1));
<a name="line320"></a>  assertEquals(203, message.getRepeatedUint32(1));
<a name="line321"></a>  assertEquals(&#39;204&#39;, message.getRepeatedUint64(1));
<a name="line322"></a>  assertEquals(205, message.getRepeatedSint32(1));
<a name="line323"></a>  assertEquals(&#39;206&#39;, message.getRepeatedSint64(1));
<a name="line324"></a>  assertEquals(207, message.getRepeatedFixed32(1));
<a name="line325"></a>  assertEquals(&#39;208&#39;, message.getRepeatedFixed64(1));
<a name="line326"></a>  assertEquals(209, message.getRepeatedSfixed32(1));
<a name="line327"></a>  assertEquals(&#39;210&#39;, message.getRepeatedSfixed64(1));
<a name="line328"></a>  assertEquals(211.5, message.getRepeatedFloat(1));
<a name="line329"></a>  assertEquals(212.5, message.getRepeatedDouble(1));
<a name="line330"></a>  assertEquals(true, message.getRepeatedBool(1));
<a name="line331"></a>  assertEquals(&#39;test#2&#39;, message.getRepeatedString(1));
<a name="line332"></a>  assertEquals(&#39;efgh&#39;, message.getRepeatedBytes(1));
<a name="line333"></a>  assertEquals(group2, message.getRepeatedgroup(1));
<a name="line334"></a>  assertEquals(nestedMessage2, message.getRepeatedNestedMessage(1));
<a name="line335"></a>  assertEquals(proto2.TestAllTypes.NestedEnum.BAR,
<a name="line336"></a>               message.getRepeatedNestedEnum(1));
<a name="line337"></a>
<a name="line338"></a>  // Check the array lengths.
<a name="line339"></a>  assertEquals(2, message.repeatedInt32Array().length);
<a name="line340"></a>  assertEquals(2, message.repeatedInt64Array().length);
<a name="line341"></a>  assertEquals(2, message.repeatedUint32Array().length);
<a name="line342"></a>  assertEquals(2, message.repeatedUint64Array().length);
<a name="line343"></a>  assertEquals(2, message.repeatedSint32Array().length);
<a name="line344"></a>  assertEquals(2, message.repeatedSint64Array().length);
<a name="line345"></a>  assertEquals(2, message.repeatedFixed32Array().length);
<a name="line346"></a>  assertEquals(2, message.repeatedFixed64Array().length);
<a name="line347"></a>  assertEquals(2, message.repeatedSfixed32Array().length);
<a name="line348"></a>  assertEquals(2, message.repeatedSfixed64Array().length);
<a name="line349"></a>  assertEquals(2, message.repeatedFloatArray().length);
<a name="line350"></a>  assertEquals(2, message.repeatedDoubleArray().length);
<a name="line351"></a>  assertEquals(2, message.repeatedBoolArray().length);
<a name="line352"></a>  assertEquals(2, message.repeatedStringArray().length);
<a name="line353"></a>  assertEquals(2, message.repeatedBytesArray().length);
<a name="line354"></a>  assertEquals(2, message.repeatedgroupArray().length);
<a name="line355"></a>  assertEquals(2, message.repeatedNestedMessageArray().length);
<a name="line356"></a>  assertEquals(2, message.repeatedNestedEnumArray().length);
<a name="line357"></a>
<a name="line358"></a>  // Check the array values.
<a name="line359"></a>  assertEquals(message.getRepeatedInt32(0), message.repeatedInt32Array()[0]);
<a name="line360"></a>  assertEquals(message.getRepeatedInt64(0), message.repeatedInt64Array()[0]);
<a name="line361"></a>  assertEquals(message.getRepeatedUint32(0), message.repeatedUint32Array()[0]);
<a name="line362"></a>  assertEquals(message.getRepeatedUint64(0), message.repeatedUint64Array()[0]);
<a name="line363"></a>  assertEquals(message.getRepeatedSint32(0), message.repeatedSint32Array()[0]);
<a name="line364"></a>  assertEquals(message.getRepeatedSint64(0), message.repeatedSint64Array()[0]);
<a name="line365"></a>  assertEquals(message.getRepeatedFixed32(0),
<a name="line366"></a>               message.repeatedFixed32Array()[0]);
<a name="line367"></a>  assertEquals(message.getRepeatedFixed64(0),
<a name="line368"></a>               message.repeatedFixed64Array()[0]);
<a name="line369"></a>  assertEquals(message.getRepeatedSfixed32(0),
<a name="line370"></a>               message.repeatedSfixed32Array()[0]);
<a name="line371"></a>  assertEquals(message.getRepeatedSfixed64(0),
<a name="line372"></a>               message.repeatedSfixed64Array()[0]);
<a name="line373"></a>  assertEquals(message.getRepeatedFloat(0), message.repeatedFloatArray()[0]);
<a name="line374"></a>  assertEquals(message.getRepeatedDouble(0), message.repeatedDoubleArray()[0]);
<a name="line375"></a>  assertEquals(message.getRepeatedBool(0), message.repeatedBoolArray()[0]);
<a name="line376"></a>  assertEquals(message.getRepeatedString(0), message.repeatedStringArray()[0]);
<a name="line377"></a>  assertEquals(message.getRepeatedBytes(0), message.repeatedBytesArray()[0]);
<a name="line378"></a>  assertEquals(message.getRepeatedgroup(0), message.repeatedgroupArray()[0]);
<a name="line379"></a>  assertEquals(message.getRepeatedNestedMessage(0),
<a name="line380"></a>               message.repeatedNestedMessageArray()[0]);
<a name="line381"></a>  assertEquals(message.getRepeatedNestedEnum(0),
<a name="line382"></a>               message.repeatedNestedEnumArray()[0]);
<a name="line383"></a>
<a name="line384"></a>  assertEquals(message.getRepeatedInt32(1), message.repeatedInt32Array()[1]);
<a name="line385"></a>  assertEquals(message.getRepeatedInt64(1), message.repeatedInt64Array()[1]);
<a name="line386"></a>  assertEquals(message.getRepeatedUint32(1), message.repeatedUint32Array()[1]);
<a name="line387"></a>  assertEquals(message.getRepeatedUint64(1), message.repeatedUint64Array()[1]);
<a name="line388"></a>  assertEquals(message.getRepeatedSint32(1), message.repeatedSint32Array()[1]);
<a name="line389"></a>  assertEquals(message.getRepeatedSint64(1), message.repeatedSint64Array()[1]);
<a name="line390"></a>  assertEquals(message.getRepeatedFixed32(1),
<a name="line391"></a>               message.repeatedFixed32Array()[1]);
<a name="line392"></a>  assertEquals(message.getRepeatedFixed64(1),
<a name="line393"></a>               message.repeatedFixed64Array()[1]);
<a name="line394"></a>  assertEquals(message.getRepeatedSfixed32(1),
<a name="line395"></a>               message.repeatedSfixed32Array()[1]);
<a name="line396"></a>  assertEquals(message.getRepeatedSfixed64(1),
<a name="line397"></a>               message.repeatedSfixed64Array()[1]);
<a name="line398"></a>  assertEquals(message.getRepeatedFloat(1), message.repeatedFloatArray()[1]);
<a name="line399"></a>  assertEquals(message.getRepeatedDouble(1), message.repeatedDoubleArray()[1]);
<a name="line400"></a>  assertEquals(message.getRepeatedBool(1), message.repeatedBoolArray()[1]);
<a name="line401"></a>  assertEquals(message.getRepeatedString(1), message.repeatedStringArray()[1]);
<a name="line402"></a>  assertEquals(message.getRepeatedBytes(1), message.repeatedBytesArray()[1]);
<a name="line403"></a>  assertEquals(message.getRepeatedgroup(1), message.repeatedgroupArray()[1]);
<a name="line404"></a>  assertEquals(message.getRepeatedNestedMessage(1),
<a name="line405"></a>               message.repeatedNestedMessageArray()[1]);
<a name="line406"></a>  assertEquals(message.getRepeatedNestedEnum(1),
<a name="line407"></a>               message.repeatedNestedEnumArray()[1]);
<a name="line408"></a>}
<a name="line409"></a>
<a name="line410"></a>function testDescriptor() {
<a name="line411"></a>  var message = new proto2.TestAllTypes();
<a name="line412"></a>  var descriptor = message.getDescriptor();
<a name="line413"></a>
<a name="line414"></a>  assertEquals(&#39;TestAllTypes&#39;, descriptor.getName());
<a name="line415"></a>  assertEquals(&#39;TestAllTypes&#39;, descriptor.getFullName());
<a name="line416"></a>  assertEquals(null, descriptor.getContainingType());
<a name="line417"></a>
<a name="line418"></a>  var nestedMessage = new proto2.TestAllTypes.NestedMessage();
<a name="line419"></a>  var nestedDescriptor = nestedMessage.getDescriptor();
<a name="line420"></a>
<a name="line421"></a>  assertEquals(&#39;NestedMessage&#39;, nestedDescriptor.getName());
<a name="line422"></a>  assertEquals(&#39;TestAllTypes.NestedMessage&#39;,
<a name="line423"></a>               nestedDescriptor.getFullName());
<a name="line424"></a>  assertEquals(descriptor, nestedDescriptor.getContainingType());
<a name="line425"></a>}
<a name="line426"></a>
<a name="line427"></a>function testFieldDescriptor() {
<a name="line428"></a>  var message = new proto2.TestAllTypes();
<a name="line429"></a>  var descriptor = message.getDescriptor();
<a name="line430"></a>  var fields = descriptor.getFields();
<a name="line431"></a>
<a name="line432"></a>  assertEquals(40, fields.length);
<a name="line433"></a>
<a name="line434"></a>  // Check the containing types.
<a name="line435"></a>  for (var i = 0; i &lt; fields.length; ++i) {
<a name="line436"></a>    assertEquals(descriptor, fields[i].getContainingType());
<a name="line437"></a>  }
<a name="line438"></a>
<a name="line439"></a>  // Check the field names.
<a name="line440"></a>  assertEquals(&#39;optional_int32&#39;, fields[0].getName());
<a name="line441"></a>  assertEquals(&#39;optional_int64&#39;, fields[1].getName());
<a name="line442"></a>  assertEquals(&#39;optional_uint32&#39;, fields[2].getName());
<a name="line443"></a>  assertEquals(&#39;optional_uint64&#39;, fields[3].getName());
<a name="line444"></a>  assertEquals(&#39;optional_sint32&#39;, fields[4].getName());
<a name="line445"></a>  assertEquals(&#39;optional_sint64&#39;, fields[5].getName());
<a name="line446"></a>  assertEquals(&#39;optional_fixed32&#39;, fields[6].getName());
<a name="line447"></a>  assertEquals(&#39;optional_fixed64&#39;, fields[7].getName());
<a name="line448"></a>  assertEquals(&#39;optional_sfixed32&#39;, fields[8].getName());
<a name="line449"></a>  assertEquals(&#39;optional_sfixed64&#39;, fields[9].getName());
<a name="line450"></a>  assertEquals(&#39;optional_float&#39;, fields[10].getName());
<a name="line451"></a>  assertEquals(&#39;optional_double&#39;, fields[11].getName());
<a name="line452"></a>  assertEquals(&#39;optional_bool&#39;, fields[12].getName());
<a name="line453"></a>  assertEquals(&#39;optional_string&#39;, fields[13].getName());
<a name="line454"></a>  assertEquals(&#39;optional_bytes&#39;, fields[14].getName());
<a name="line455"></a>  assertEquals(&#39;optionalgroup&#39;, fields[15].getName());
<a name="line456"></a>  assertEquals(&#39;optional_nested_message&#39;, fields[16].getName());
<a name="line457"></a>  assertEquals(&#39;optional_nested_enum&#39;, fields[17].getName());
<a name="line458"></a>
<a name="line459"></a>  assertEquals(&#39;repeated_int32&#39;, fields[18].getName());
<a name="line460"></a>  assertEquals(&#39;repeated_int64&#39;, fields[19].getName());
<a name="line461"></a>  assertEquals(&#39;repeated_uint32&#39;, fields[20].getName());
<a name="line462"></a>  assertEquals(&#39;repeated_uint64&#39;, fields[21].getName());
<a name="line463"></a>  assertEquals(&#39;repeated_sint32&#39;, fields[22].getName());
<a name="line464"></a>  assertEquals(&#39;repeated_sint64&#39;, fields[23].getName());
<a name="line465"></a>  assertEquals(&#39;repeated_fixed32&#39;, fields[24].getName());
<a name="line466"></a>  assertEquals(&#39;repeated_fixed64&#39;, fields[25].getName());
<a name="line467"></a>  assertEquals(&#39;repeated_sfixed32&#39;, fields[26].getName());
<a name="line468"></a>  assertEquals(&#39;repeated_sfixed64&#39;, fields[27].getName());
<a name="line469"></a>  assertEquals(&#39;repeated_float&#39;, fields[28].getName());
<a name="line470"></a>  assertEquals(&#39;repeated_double&#39;, fields[29].getName());
<a name="line471"></a>  assertEquals(&#39;repeated_bool&#39;, fields[30].getName());
<a name="line472"></a>  assertEquals(&#39;repeated_string&#39;, fields[31].getName());
<a name="line473"></a>  assertEquals(&#39;repeated_bytes&#39;, fields[32].getName());
<a name="line474"></a>  assertEquals(&#39;repeatedgroup&#39;, fields[33].getName());
<a name="line475"></a>  assertEquals(&#39;repeated_nested_message&#39;, fields[34].getName());
<a name="line476"></a>  assertEquals(&#39;repeated_nested_enum&#39;, fields[35].getName());
<a name="line477"></a>
<a name="line478"></a>  assertEquals(&#39;optional_int64_number&#39;, fields[36].getName());
<a name="line479"></a>  assertEquals(&#39;optional_int64_string&#39;, fields[37].getName());
<a name="line480"></a>  assertEquals(&#39;repeated_int64_number&#39;, fields[38].getName());
<a name="line481"></a>  assertEquals(&#39;repeated_int64_string&#39;, fields[39].getName());
<a name="line482"></a>
<a name="line483"></a>  // Check the field types.
<a name="line484"></a>  var FieldType = goog.proto2.FieldDescriptor.FieldType;
<a name="line485"></a>  assertEquals(FieldType.INT32, fields[0].getFieldType());
<a name="line486"></a>  assertEquals(FieldType.INT64, fields[1].getFieldType());
<a name="line487"></a>  assertEquals(FieldType.UINT32, fields[2].getFieldType());
<a name="line488"></a>  assertEquals(FieldType.UINT64, fields[3].getFieldType());
<a name="line489"></a>  assertEquals(FieldType.SINT32, fields[4].getFieldType());
<a name="line490"></a>  assertEquals(FieldType.SINT64, fields[5].getFieldType());
<a name="line491"></a>  assertEquals(FieldType.FIXED32, fields[6].getFieldType());
<a name="line492"></a>  assertEquals(FieldType.FIXED64, fields[7].getFieldType());
<a name="line493"></a>  assertEquals(FieldType.SFIXED32, fields[8].getFieldType());
<a name="line494"></a>  assertEquals(FieldType.SFIXED64, fields[9].getFieldType());
<a name="line495"></a>  assertEquals(FieldType.FLOAT, fields[10].getFieldType());
<a name="line496"></a>  assertEquals(FieldType.DOUBLE, fields[11].getFieldType());
<a name="line497"></a>  assertEquals(FieldType.BOOL, fields[12].getFieldType());
<a name="line498"></a>  assertEquals(FieldType.STRING, fields[13].getFieldType());
<a name="line499"></a>  assertEquals(FieldType.BYTES, fields[14].getFieldType());
<a name="line500"></a>  assertEquals(FieldType.GROUP, fields[15].getFieldType());
<a name="line501"></a>  assertEquals(FieldType.MESSAGE, fields[16].getFieldType());
<a name="line502"></a>  assertEquals(FieldType.ENUM, fields[17].getFieldType());
<a name="line503"></a>
<a name="line504"></a>  assertEquals(FieldType.INT32, fields[18].getFieldType());
<a name="line505"></a>  assertEquals(FieldType.INT64, fields[19].getFieldType());
<a name="line506"></a>  assertEquals(FieldType.UINT32, fields[20].getFieldType());
<a name="line507"></a>  assertEquals(FieldType.UINT64, fields[21].getFieldType());
<a name="line508"></a>  assertEquals(FieldType.SINT32, fields[22].getFieldType());
<a name="line509"></a>  assertEquals(FieldType.SINT64, fields[23].getFieldType());
<a name="line510"></a>  assertEquals(FieldType.FIXED32, fields[24].getFieldType());
<a name="line511"></a>  assertEquals(FieldType.FIXED64, fields[25].getFieldType());
<a name="line512"></a>  assertEquals(FieldType.SFIXED32, fields[26].getFieldType());
<a name="line513"></a>  assertEquals(FieldType.SFIXED64, fields[27].getFieldType());
<a name="line514"></a>  assertEquals(FieldType.FLOAT, fields[28].getFieldType());
<a name="line515"></a>  assertEquals(FieldType.DOUBLE, fields[29].getFieldType());
<a name="line516"></a>  assertEquals(FieldType.BOOL, fields[30].getFieldType());
<a name="line517"></a>  assertEquals(FieldType.STRING, fields[31].getFieldType());
<a name="line518"></a>  assertEquals(FieldType.BYTES, fields[32].getFieldType());
<a name="line519"></a>  assertEquals(FieldType.GROUP, fields[33].getFieldType());
<a name="line520"></a>  assertEquals(FieldType.MESSAGE, fields[34].getFieldType());
<a name="line521"></a>  assertEquals(FieldType.ENUM, fields[35].getFieldType());
<a name="line522"></a>
<a name="line523"></a>  assertEquals(FieldType.INT64, fields[36].getFieldType());
<a name="line524"></a>  assertEquals(FieldType.INT64, fields[37].getFieldType());
<a name="line525"></a>  assertEquals(FieldType.INT64, fields[38].getFieldType());
<a name="line526"></a>  assertEquals(FieldType.INT64, fields[39].getFieldType());
<a name="line527"></a>
<a name="line528"></a>  // Check the field native types.
<a name="line529"></a>  // Singular.
<a name="line530"></a>  assertEquals(Number, fields[0].getNativeType());
<a name="line531"></a>  assertEquals(String, fields[1].getNativeType()); // 64 bit values are strings.
<a name="line532"></a>  assertEquals(Number, fields[2].getNativeType());
<a name="line533"></a>  assertEquals(String, fields[3].getNativeType());
<a name="line534"></a>  assertEquals(Number, fields[4].getNativeType());
<a name="line535"></a>  assertEquals(String, fields[5].getNativeType());
<a name="line536"></a>  assertEquals(Number, fields[6].getNativeType());
<a name="line537"></a>  assertEquals(String, fields[7].getNativeType());
<a name="line538"></a>  assertEquals(Number, fields[8].getNativeType());
<a name="line539"></a>  assertEquals(String, fields[9].getNativeType());
<a name="line540"></a>  assertEquals(Number, fields[10].getNativeType());
<a name="line541"></a>  assertEquals(Number, fields[11].getNativeType());
<a name="line542"></a>
<a name="line543"></a>  assertEquals(Boolean, fields[12].getNativeType());
<a name="line544"></a>
<a name="line545"></a>  assertEquals(String, fields[13].getNativeType());
<a name="line546"></a>  assertEquals(String, fields[14].getNativeType());
<a name="line547"></a>
<a name="line548"></a>  assertEquals(proto2.TestAllTypes.OptionalGroup, fields[15].getNativeType());
<a name="line549"></a>  assertEquals(proto2.TestAllTypes.NestedMessage, fields[16].getNativeType());
<a name="line550"></a>  assertEquals(proto2.TestAllTypes.NestedEnum, fields[17].getNativeType());
<a name="line551"></a>
<a name="line552"></a>  assertEquals(Number, fields[36].getNativeType());  // [jstype=&quot;number&quot;]
<a name="line553"></a>  assertEquals(String, fields[37].getNativeType());
<a name="line554"></a>
<a name="line555"></a>  // Repeated.
<a name="line556"></a>  assertEquals(Number, fields[18].getNativeType());
<a name="line557"></a>  assertEquals(String, fields[19].getNativeType());
<a name="line558"></a>  assertEquals(Number, fields[20].getNativeType());
<a name="line559"></a>  assertEquals(String, fields[21].getNativeType());
<a name="line560"></a>  assertEquals(Number, fields[22].getNativeType());
<a name="line561"></a>  assertEquals(String, fields[23].getNativeType());
<a name="line562"></a>  assertEquals(Number, fields[24].getNativeType());
<a name="line563"></a>  assertEquals(String, fields[25].getNativeType());
<a name="line564"></a>  assertEquals(Number, fields[26].getNativeType());
<a name="line565"></a>  assertEquals(String, fields[27].getNativeType());
<a name="line566"></a>  assertEquals(Number, fields[28].getNativeType());
<a name="line567"></a>  assertEquals(Number, fields[29].getNativeType());
<a name="line568"></a>
<a name="line569"></a>  assertEquals(Boolean, fields[30].getNativeType());
<a name="line570"></a>
<a name="line571"></a>  assertEquals(String, fields[31].getNativeType());
<a name="line572"></a>  assertEquals(String, fields[32].getNativeType());
<a name="line573"></a>
<a name="line574"></a>  assertEquals(proto2.TestAllTypes.RepeatedGroup, fields[33].getNativeType());
<a name="line575"></a>  assertEquals(proto2.TestAllTypes.NestedMessage, fields[34].getNativeType());
<a name="line576"></a>  assertEquals(proto2.TestAllTypes.NestedEnum, fields[35].getNativeType());
<a name="line577"></a>
<a name="line578"></a>  assertEquals(Number, fields[38].getNativeType());  // [jstype=&quot;number&quot;]
<a name="line579"></a>  assertEquals(String, fields[39].getNativeType());
<a name="line580"></a>}
<a name="line581"></a>
<a name="line582"></a>function testUnknown() {
<a name="line583"></a>  var message = new proto2.TestAllTypes();
<a name="line584"></a>
<a name="line585"></a>  // Set some unknown fields.
<a name="line586"></a>  message.setUnknown(1000, 101);
<a name="line587"></a>  message.setUnknown(1001, -102);
<a name="line588"></a>  message.setUnknown(1002, true);
<a name="line589"></a>  message.setUnknown(1003, &#39;abcd&#39;);
<a name="line590"></a>  message.setUnknown(1004, [&#39;he&#39;, &#39;llo&#39;]);
<a name="line591"></a>
<a name="line592"></a>  // Ensure we find them all.
<a name="line593"></a>  var count = 0;
<a name="line594"></a>
<a name="line595"></a>  message.forEachUnknown(function(tag, value) {
<a name="line596"></a>    if (tag == 1000) {
<a name="line597"></a>      assertEquals(101, value);
<a name="line598"></a>    }
<a name="line599"></a>
<a name="line600"></a>    if (tag == 1001) {
<a name="line601"></a>      assertEquals(-102, value);
<a name="line602"></a>    }
<a name="line603"></a>
<a name="line604"></a>    if (tag == 1002) {
<a name="line605"></a>      assertEquals(true, value);
<a name="line606"></a>    }
<a name="line607"></a>
<a name="line608"></a>    if (tag == 1003) {
<a name="line609"></a>      assertEquals(&#39;abcd&#39;, value);
<a name="line610"></a>    }
<a name="line611"></a>
<a name="line612"></a>    if (tag == 1004) {
<a name="line613"></a>      assertEquals(&#39;he&#39;, value[0]);
<a name="line614"></a>      assertEquals(&#39;llo&#39;, value[1]);
<a name="line615"></a>    }
<a name="line616"></a>
<a name="line617"></a>    count++;
<a name="line618"></a>  });
<a name="line619"></a>
<a name="line620"></a>  assertEquals(5, count);
<a name="line621"></a>}
<a name="line622"></a>
<a name="line623"></a>function testReflection() {
<a name="line624"></a>  var message = new proto2.TestAllTypes();
<a name="line625"></a>  var descriptor = message.getDescriptor();
<a name="line626"></a>  var optionalInt = descriptor.findFieldByName(&#39;optional_int32&#39;);
<a name="line627"></a>  var optionalString = descriptor.findFieldByName(&#39;optional_string&#39;);
<a name="line628"></a>  var repeatedInt64 = descriptor.findFieldByName(&#39;repeated_int64&#39;);
<a name="line629"></a>  var optionalWrong = descriptor.findFieldByName(&#39;foo_bar&#39;);
<a name="line630"></a>
<a name="line631"></a>  assertFalse(optionalInt == null);
<a name="line632"></a>  assertFalse(optionalString == null);
<a name="line633"></a>  assertFalse(repeatedInt64 == null);
<a name="line634"></a>  assertTrue(optionalWrong == null);
<a name="line635"></a>
<a name="line636"></a>  // Check to ensure the fields are empty.
<a name="line637"></a>  assertFalse(message.has(optionalInt));
<a name="line638"></a>  assertFalse(message.has(optionalString));
<a name="line639"></a>  assertFalse(message.has(repeatedInt64));
<a name="line640"></a>
<a name="line641"></a>  assertEquals(0, message.arrayOf(repeatedInt64).length);
<a name="line642"></a>
<a name="line643"></a>  // Check default values.
<a name="line644"></a>  assertEquals(0, message.getOrDefault(optionalInt));
<a name="line645"></a>  assertEquals(&#39;&#39;, message.getOrDefault(optionalString));
<a name="line646"></a>
<a name="line647"></a>  // Set some of the fields.
<a name="line648"></a>  message.set(optionalString, &#39;hello!&#39;);
<a name="line649"></a>
<a name="line650"></a>  message.add(repeatedInt64, &#39;101&#39;);
<a name="line651"></a>  message.add(repeatedInt64, &#39;102&#39;);
<a name="line652"></a>
<a name="line653"></a>  // Check the fields.
<a name="line654"></a>  assertFalse(message.has(optionalInt));
<a name="line655"></a>
<a name="line656"></a>  assertTrue(message.has(optionalString));
<a name="line657"></a>  assertTrue(message.hasOptionalString());
<a name="line658"></a>
<a name="line659"></a>  assertTrue(message.has(repeatedInt64));
<a name="line660"></a>  assertTrue(message.hasRepeatedInt64());
<a name="line661"></a>
<a name="line662"></a>  // Check the values.
<a name="line663"></a>  assertEquals(&#39;hello!&#39;, message.get(optionalString));
<a name="line664"></a>  assertEquals(&#39;hello!&#39;, message.getOptionalString());
<a name="line665"></a>
<a name="line666"></a>  assertEquals(&#39;101&#39;, message.get(repeatedInt64, 0));
<a name="line667"></a>  assertEquals(&#39;102&#39;, message.get(repeatedInt64, 1));
<a name="line668"></a>
<a name="line669"></a>  assertEquals(&#39;101&#39;, message.getRepeatedInt64(0));
<a name="line670"></a>  assertEquals(&#39;102&#39;, message.getRepeatedInt64(1));
<a name="line671"></a>
<a name="line672"></a>  // Check the count.
<a name="line673"></a>  assertEquals(0, message.countOf(optionalInt));
<a name="line674"></a>
<a name="line675"></a>  assertEquals(1, message.countOf(optionalString));
<a name="line676"></a>  assertEquals(1, message.optionalStringCount());
<a name="line677"></a>
<a name="line678"></a>  assertEquals(2, message.countOf(repeatedInt64));
<a name="line679"></a>  assertEquals(2, message.repeatedInt64Count());
<a name="line680"></a>
<a name="line681"></a>  // Check the array.
<a name="line682"></a>  assertEquals(2, message.arrayOf(repeatedInt64).length);
<a name="line683"></a>
<a name="line684"></a>  assertEquals(message.get(repeatedInt64, 0),
<a name="line685"></a>      message.arrayOf(repeatedInt64)[0]);
<a name="line686"></a>
<a name="line687"></a>  assertEquals(message.get(repeatedInt64, 1),
<a name="line688"></a>      message.arrayOf(repeatedInt64)[1]);
<a name="line689"></a>}
<a name="line690"></a>
<a name="line691"></a>function testDefaultValuesForMessages_actualBehavior() {
<a name="line692"></a>  var message = new proto2.TestDefaultParent();
<a name="line693"></a>  // The default child message is shared between all TestDefaultParent messges.
<a name="line694"></a>  // Ideally this object would be immutable, but the current API does not
<a name="line695"></a>  // enforce that behavior.
<a name="line696"></a>  var child = message.getChildOrDefault();
<a name="line697"></a>  child.setFoo(false);
<a name="line698"></a>  // Changing the value returned by get**OrDefault does not actually change
<a name="line699"></a>  // the value stored in the parent message.
<a name="line700"></a>  assertFalse(message.hasChild());
<a name="line701"></a>  assertNull(message.getChild());
<a name="line702"></a>
<a name="line703"></a>  var message2 = new proto2.TestDefaultParent();
<a name="line704"></a>  var child2 = message2.getChildOrDefault();
<a name="line705"></a>  assertNull(message2.getChild());
<a name="line706"></a>  // The parent message returns the same default object
<a name="line707"></a>  assertEquals(child, child2);
<a name="line708"></a>  // You&#39;ve changed the value of the underlying default message
<a name="line709"></a>  // So changes will be reflected.
<a name="line710"></a>  assertTrue(child2.hasFoo());
<a name="line711"></a>  assertFalse(child2.getFooOrDefault());
<a name="line712"></a>}
<a name="line713"></a>
<a name="line714"></a>function testDefaultValues_bestPractice() {
<a name="line715"></a>  var message = new proto2.TestDefaultParent();
<a name="line716"></a>  // If you&#39;re unsure whether or not a message field has
<a name="line717"></a>  // been set this is the best way to get the message.
<a name="line718"></a>  var child = message.hasChild() ? message.getChild() :
<a name="line719"></a>      new proto2.TestDefaultChild();
<a name="line720"></a>  child.setFoo(false);
<a name="line721"></a>  assertFalse(message.hasChild());
<a name="line722"></a>  assertNull(message.getChild());
<a name="line723"></a>  message.setChild(child);
<a name="line724"></a>  assertFalse(message.getChildOrDefault().getFoo());
<a name="line725"></a>
<a name="line726"></a>  var message2 = new proto2.TestDefaultParent();
<a name="line727"></a>  var child2 = message2.getChildOrDefault();
<a name="line728"></a>  assertNotEquals(child, child2);
<a name="line729"></a>  assertTrue(child2.getFooOrDefault());
<a name="line730"></a>}
<a name="line731"></a>
<a name="line732"></a>function testDefaultValuesForMessages_worstPractice() {
<a name="line733"></a>  var message = new proto2.TestDefaultParent();
<a name="line734"></a>  var child = message.getChildOrDefault();
<a name="line735"></a>
<a name="line736"></a>  // This is a terrible idea
<a name="line737"></a>  message.setChild(child);
<a name="line738"></a>  message.getChild().setFoo(false);
<a name="line739"></a>  // Because changes to this message now sync across all default messages
<a name="line740"></a>  var message2 = new proto2.TestDefaultParent();
<a name="line741"></a>  assertFalse(message2.getChildOrDefault().getFooOrDefault());
<a name="line742"></a>  message.getChild().setFoo(true);
<a name="line743"></a>  assertTrue(message2.getChildOrDefault().getFooOrDefault());
<a name="line744"></a>  assertEquals(message.getChild(), message2.getChildOrDefault());
<a name="line745"></a>}
</pre>


</body>
</html>
